package com.scsj.supermarket.view.activity.serchmodel;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.NestedScrollView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gigamole.navigationtabstrip.NavigationTabStrip;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.MainFragmentAdapter;
import com.scsj.supermarket.bean.QueryShopInTradeBean;
import com.scsj.supermarket.utils.DialogUtils;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.utils.ToastMiddleUtils;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;
import com.scsj.supermarket.view.fragment.basemodel.BaseFragment;
import com.scsj.supermarket.view.fragment.twokindsbasemodel.GoodsListBaseFragment;
import com.scsj.supermarket.view.fragment.twokindsbasemodel.ShopListBaseFragment;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;

/**
 * 地图搜索结果页面
 */

public class MapSearchResultActivity extends BaseActivity implements View.OnClickListener {

    private NavigationTabStrip mTopNavigationTabStrip;
    private ViewPager mViewPager;
    private ArrayList<BaseFragment> fragmentList;
    private MainFragmentAdapter mAdpter;
    private ImageView morePic;
    private EditText searchShopOrGoodsEt;
    private RelativeLayout startSearchRl;
    private ImageView topAndRightIv;
    private String[] title = {"店铺", "商品"};
    private String tradeId;
    private String tradePhone;
    private PopupWindow popupWindow;
    private GoodsListBaseFragment goodsListBaseFragment;
    private ShopListBaseFragment shopListBaseFragment;

    private ViewPager viewPager;
    private NestedScrollView scrollView;

    //用来存放商圈下所有店铺的集合
    List<QueryShopInTradeBean.DataBean.ListBean> shopInTrades = new ArrayList<>();

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.map_circle_and_shop_layout);
        initView();
        initListener();
        initData();
    }

    @Override
    protected void initView() {
        viewPager = findViewById(R.id.viewpager);
        scrollView = findViewById(R.id.scroll);
        topAndRightIv = findViewById(R.id.toolbar_right_iv);
        startSearchRl = findViewById(R.id.start_search_rl);
        searchShopOrGoodsEt = findViewById(R.id.et_search_key);
        morePic = findViewById(R.id.toolbar_right_iv);
        morePic.setVisibility(View.VISIBLE);
        initToolbar();
        intiNavigationBar();


    }

    @Override
    protected void initListener() {
        startSearchRl.setOnClickListener(this);
        topAndRightIv.setOnClickListener(this);
        searchShopOrGoodsEt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //显示光标
                searchShopOrGoodsEt.setCursorVisible(true);
            }
        });
    }

    @Override
    protected void initData() {
        tradeId =getIntent().getStringExtra("tradeId");
        tradePhone =getIntent().getStringExtra("tradePhone");

        tradeId = getIntent().getStringExtra("tradeId");
        tradePhone = getIntent().getStringExtra("tradePhone");
    }

    private void intiNavigationBar() {

        mTopNavigationTabStrip = (NavigationTabStrip) findViewById(R.id.mediator_tab);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setOffscreenPageLimit(2);

        goodsListBaseFragment = new GoodsListBaseFragment();
        shopListBaseFragment = new ShopListBaseFragment();

        fragmentList = new ArrayList<>();

        fragmentList.add(goodsListBaseFragment);
        fragmentList.add(shopListBaseFragment);

        mAdpter = new MainFragmentAdapter(getSupportFragmentManager(), fragmentList);
        mViewPager.setAdapter(mAdpter);

        mTopNavigationTabStrip.setViewPager(mViewPager, 0);
        mTopNavigationTabStrip.setTitles(title);
        mTopNavigationTabStrip.setStripType(NavigationTabStrip.StripType.LINE);
        mTopNavigationTabStrip.setStripGravity(NavigationTabStrip.StripGravity.BOTTOM);
        mTopNavigationTabStrip.setTabIndex(0, true);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_search_rl:
                //点击搜索按钮
                String searchText = searchShopOrGoodsEt.getText().toString().trim();

                //开始搜索
                shopListBaseFragment.searchText = searchText;
                goodsListBaseFragment.searchText = searchText;
                shopListBaseFragment.startGetData();
                goodsListBaseFragment.startGetData();

                break;

            case R.id.toolbar_right_iv:
                //显示弹框
                showPopWindow();
                break;
        }

    }

    private void showPopWindow() {
        //设置背景半透明
        backgroundAlpha(0.5f);
        View contentView = LayoutInflater.from(this).inflate(R.layout.trade_pop_layout, null);

        popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);
        // 如果不设置PopupWindow的背景，有些版本就会出现一个问题：无论是点击外部区域还是Back键都无法dismiss弹框
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        // 设置好参数之后再show
        popupWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = popupWindow.getContentView().getMeasuredWidth();
        popupWindow.showAsDropDown(topAndRightIv, 70 - popupWidth, 64);
        popupWindow.setOnDismissListener(new MapSearchResultActivity.popupDismissListener());

        //点击我的购物车
        LinearLayout mineCart = contentView.findViewById(R.id.mine_cart);
        mineCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        //分享店铺
        LinearLayout shareShop = contentView.findViewById(R.id.share_shop);
        shareShop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
                share();
            }
        });
        //商圈详情
        LinearLayout tradeDetail = contentView.findViewById(R.id.trade_detail);
        tradeDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SkipUtils.toBusinessCircleDetail(MapSearchResultActivity.this, tradeId);

            }
        });
        //联系圈主
        LinearLayout commicateShoper = contentView.findViewById(R.id.tel_shoper);
        commicateShoper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tradePhone));//跳转到拨号界面，同时传递电话号码
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toasty.info(MapSearchResultActivity.this, "无法拨打电话，请打开拨打电话的权限").show();
                }
            }
        });


    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();

        lp.alpha = bgAlpha; //0.0-1.0
        this.getWindow().setAttributes(lp);
    }

    /**
     * /**
     * 将背景透明度改回来
     */
    class popupDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            backgroundAlpha(1f);
        }

    }

    private void share() {

        DialogUtils.getInstance()
                .with(this)//上下文
                .setlayoutId(R.layout.make_money_pop_window_layout)//布局文件
                .setlayoutPosition(Gravity.BOTTOM)//位置
                .setlayoutAnimaType(0)//动画类型:0自下而上动画，1淡入淡出动画
                .setlayoutPading(20, 0, 20, 20)//与屏幕距离
                .setOnChildViewclickListener(new DialogUtils.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {
                        //处理dialog内部控件的点击事件
                        LinearLayout weichat_share_ll = view.findViewById(R.id.weichat_share_ll);
                        weichat_share_ll.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ToastMiddleUtils.ToastMiddle(MapSearchResultActivity.this, "微信");
                            }
                        });
                        TextView disMissTv = view.findViewById(R.id.share_dismiss_tv);
                        disMissTv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                DialogUtils.dismiss();
                            }
                        });
                    }
                })
                .show();

    }

    @Override
    protected void onStop() {
        super.onStop();
        popupWindow.dismiss();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        popupWindow.dismiss();
    }
}
